/*******************************************************************\
*	file: 		NBPRegisterName.c									*
*	version:	1.06§ 												*
* 																	*
*	Register this node under the name defined in the chooser's 		*
*	name resource.													*
* -----------------------------------------------------------------	*
* By:	Donald Koscheka, Greg Kimberly								*
* Date:	21-Sept-87													*
* ©	Copyright 1987, Apple Computer, Inc.							*
*	All Rights Reserved												*
*																	*
* -----------------------------------------------------------------	*
*						Modification History						*
* -----------------------------------------------------------------	*
*  Date	   	| By	|	 				Description					*
* -----------------------------------------------------------------	*
* 9/21/87	| GK	| file created									*
* 10/6/87	| DK	| added parameter passing for name and type		*
*  5-Nov-87	| DK	| added return result							*
*  8-Dec-87	| DK	| added verify flag to input parameter list		*
* 21-Dec-87	| DK	| added "empty" name support					*
* 14-Jan-87	| DK	| modified to reflect decoupling of NBP & ATP	*
*			| DK	| allows you to register only once 				*
*			|		| (temporary patch for bug fix)					*
* -----------------------------------------------------------------	*
\*******************************************************************/


/*******************************************************************\
							Build Sequence
	
C -q2 -g -o "{hpo}"NBPRegisterName.c.o "{nbp}"NBPRegisterName.c
	link  -sn Main=NBPRegisterName -sn STDIO=NBPRegisterName ¶
		 -sn INTENV=NBPRegisterName -rt XCMD=305 ¶
		 -m NBPREGISTERNAME ¶
		 "{hpo}"NBPRegisterName.c.o  "{hpo}"atalkxcmd.c.o "{hpo}"xcmdutils.c.o ¶
		 "{CLibraries}"CInterface.o ¶
		 "{Libraries}"Interface.o ¶
		 -o "{hp}"HyperPeople

\*******************************************************************/

#include <Types.h>
#include <Memory.h>
#include <Resources.h>
#include <OSUtils.h>
#include <appleTalk.h>
#include <HyperXCmd.h>
#include <atalkXCMD.h>
#include <XCMDUtils.h>


pascal void NBPRegisterName(paramPtr)
	XCmdBlockPtr	paramPtr;
/**********************************
* Register this entity on the network
* using the name and type specified.
*
*	params[0]	char	*theName
*	params[1]	char	*theType
*	params[2]	short	count
*	params[3]	short	interval
*	params[4]	short	verifyFlag
* -----------------------------------
*
* defaults are: name	= (from chooser)
*				type	= 'HyperPeople'
*				count	= 2
*				interval= 8
*				verify	= false;
*
* NOTE: will not register if a socket
* is not open on this node!
**********************************/
{
	NBPBlock 	*nbp;
	char		theName[32], theType[32], temp[32], str[32];
	Handle 		myType	= nil;
	Handle		myName	= nil;
	Handle		saveName;
	Handle 		DataHandle;
	short		count, interval, verify;
	short		result = DEFAULT_ERROR;
	long		len;
	long		addrData;
	AddrBlock	*addr;
	
	nbp = (NBPBlock *)RetrieveHandle( paramPtr, GLOBALNBPDATA );
	
	if( !nbp || nbp->Registered ){
		paramPtr->returnValue = ErrorReturn( DEFAULT_ERROR );
		return;
	}
	
	/*** Before we can register, we need to get the internet address ***/
	addr 		 = &(nbp->NTEntry.nt.nteAddress);
	addrData 	 = RetrieveHandle( paramPtr, GLOBALSKTDATA );

	addr->aSocket= (unsigned char)	(0x000000FF & addrData);
	addr->aNet	 = (unsigned char)	(0x0000FF00 & addrData) >> 0x08;
	addr->aNode  = (short)			(0xFFFF0000 & addrData) >> 0x10;

	if( (paramPtr->params[0] == nil) || !(**(paramPtr->params[0])) ){	
		if( myName = GetResource('STR ', EntityNameStr ) ){
			pStrCopy( *myName, theName );
		}
		else{		/*** no name was given with which to register ***/
			paramPtr->returnValue = ErrorReturn( DEFAULT_ERROR );
			return;
		}
	}
	else{
		len = GetHandleSize( paramPtr->params[0] );
		BlockMove( *(paramPtr->params[0]), theName, len );
		c2pstr( theName );
	}
		
	/*** save the registered name off in a global variable ***/
	saveName = GetResource( 'STR ', myEntityNameStr	 );
	if( saveName ){
		HLock( saveName );
		DataHandle = PasToZero( paramPtr, theName );
		SetGlobal( paramPtr, *saveName, DataHandle );	
		DisposHandle( DataHandle );
		HUnlock( saveName );
	}
	
	if( paramPtr->params[1] == nil || !(**(paramPtr->params[1])) ){
		if( myType = GetResource('STR ', EntityTypeStr ) ){
			pStrCopy( *myType, theType );
		}
		else{
			paramPtr->returnValue = ErrorReturn(  DEFAULT_ERROR );
			return;
		}
	}
	else{
		len = GetHandleSize( paramPtr->params[1] );
		BlockMove( *(paramPtr->params[1]), theType, len );
		c2pstr( theType );
	}

	if( paramPtr->params[2] != nil ){
		len = GetHandleSize( paramPtr->params[2] );	
		BlockMove( *(paramPtr->params[2]), temp, len );
		c2pstr( temp );
		count = (short)StrToNum( paramPtr, temp );
	}
	else
		count = 2;	
	
	if( paramPtr->params[3] != nil ){	
		len = GetHandleSize( paramPtr->params[3] );	
		BlockMove( (*paramPtr->params[3]), temp, len );
		c2pstr( temp );
		interval = (short)StrToNum( paramPtr, temp );
	}
	else
		interval = 8;
	
	verify = false;
	MakeAnswer( true, str );
		
	if( paramPtr->params[4] != nil )
		if( (strCMP( str, (*paramPtr->params[4]) )) == 0 ) 
			verify = true;
		
	result = NodeRegister( nbp, theName, theType, count, interval, verify, addr );
	
	paramPtr->returnValue = ErrorReturn( result );
}

